package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 设备Service业务层处理
 * 
 * @author yunyao
 * @date 2024-10-14
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Resource
    private ApplicationContext applicationContext;

    /**
     * 查询设备
     * 
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备列表
     * 
     * @param vendingMachine 设备
     * @return 设备
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备
     * 
     * @param vendingMachine 设备
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //id
        vendingMachine.setInnerCode(UUIDUtils.getUUID());
        //查询 设备类型表 补充 最大货道数：ChannelMaxCapacity
        VmTypeMapper vmTypeMapper = applicationContext.getBean(VmTypeMapper.class);
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //查询 点位表 补充字段信息 详细地址：addr
        NodeMapper nodeMapper = applicationContext.getBean(NodeMapper.class);
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setAddr(node.getAddress());
        vendingMachine.setBusinessType(node.getBusinessType());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setPartnerId(node.getPartnerId());
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(new Date());
        int rows = vendingMachineMapper.insertVendingMachine(vendingMachine);

        //新增货道
        ChannelMapper channelMapper = applicationContext.getBean(ChannelMapper.class);
        List<Channel> list = new ArrayList();

        for (int i = 1; i <= vmType.getVmRow() ; i++) {
            for (int j = 1; j <= vmType.getVmCol();j++){
                Channel channel = new Channel();
                channel.setChannelCode(i+"-"+j); //货道编号
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
                channel.setCreateTime(new Date());
                channel.setUpdateTime(new Date());
                list.add(channel);
            }
        }
        channelMapper.batchInsertChannels(list);
        return rows;
    }

    /**
     * 修改设备
     * 
     * @param vendingMachine 设备
     * @return 结果
     */
    //region 修改设备
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        if (vendingMachine.getNodeId() != null){
            //查询点位表，补充信息
            NodeMapper nodeMapper = applicationContext.getBean(NodeMapper.class);
            Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
            vendingMachine.setAddr(node.getAddress());
            //借助糊涂工具包将 点位表 的字段复制到 设备表
            BeanUtil.copyProperties(node,vendingMachine,"id");
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }
    //endregion
    /**
     * 批量删除设备
     * 
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备信息
     * 
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 感觉设备编号返回设备信息
     * @param innerCode
     * @return VendingMachine
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
